import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/*
 * Team Huskies: Aakanksha Gaur, Alice Robinson, Paul Winters, and Steven Bradshaw
 * Course: TCSS 422
 * Assignment: Project 1
 * Date: April 26th 2011
 */

/**
 * The custom JPanel class used to draw the GUI
 * borrowed and modified from another of Alice's projects.
 * @author Team Huskies: Aakanksha Gaur, Alice Robinson, Paul Winters, and Steven Bradshaw
 * @version 1, April 26th 2011
 */
@SuppressWarnings("serial")
public class WebPanel extends JPanel
{
	/**
	 * The activity marker.
	 */
	BufferedImage my_key_spiner;
	
	/**
	 * Variations on the activity marker.
	 */
	BufferedImage my_spiner;
	BufferedImage my_spiner1;
	BufferedImage my_spiner2;
	BufferedImage my_spiner3;
	
	/**
	 * The current rotation of the activity marker.
	 */
	int my_current_rotation;
	
	/**
	 * The main part of the display.
	 */
	String my_large_string;
	
	/**
	 * The details of the hits.
	 */
	String my_detail_string;
	
	/**
	 * The constructor for WebPanel
	 */
	public WebPanel()
	{
		
		try {
			my_spiner = ImageIO.read(new File("src/spinner.png"));
			my_spiner1 = ImageIO.read(new File("src/spinner1.png"));
			my_spiner2 = ImageIO.read(new File("src/spinner2.png"));
			my_spiner3 = ImageIO.read(new File("src/spinner3.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		my_detail_string = "";
		my_large_string = "";
		my_current_rotation = 0;
		my_key_spiner = my_spiner;
	}
	
	/**
	 * Controls how to paint the JPanel.
	 */
	public void paintComponent(final Graphics a_gr)
	{
		super.paintComponent(a_gr);
	    final Graphics2D a_gr2d = (Graphics2D) a_gr;
	    a_gr2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,   
			    RenderingHints.VALUE_ANTIALIAS_ON);
	    
	    Font my_font = new Font("Serif",Font.ITALIC , 40);
	    a_gr2d.setFont(my_font);      
	    a_gr2d.setColor(Color.BLACK);	    
	    a_gr2d.drawString("Crawling the web...", 5, 40);    
	    a_gr2d.setStroke(new BasicStroke(4));
	    a_gr2d.drawImage(my_key_spiner, 720, 520, null);
	    my_font = new Font("Monospace",Font.PLAIN , 20);
	    a_gr2d.setFont(my_font);
	    
	    a_gr2d.drawLine(0, 65, 790, 65);
	    drawCutString(a_gr2d, my_large_string, 10, 70);
	    drawCutString(a_gr2d, my_detail_string, 200, 200);
	}

	/**
	 * Allows \n to be used as a newline marker in drawString() which is
	 * not built in functionality.
	 * 
	 * @param g The Graphics to be used.
	 * @param text The text to but split
	 * @param x The x coordinate to print at
	 * @param y The y coordinate to print at
	 */
	private void drawCutString(Graphics g, String text, int x, int y) 
	{
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }
	
	/**
	 * Sets the strings to print
	 * 
	 * @param the_passed_string The big string to print
	 * @param the_second_passed_string The details to print
	 */
	public void setBigString(String the_passed_string, String the_second_passed_string)
	{
		my_large_string = the_passed_string;
		my_detail_string = the_second_passed_string;
		spin();
		repaint();
	}
	
	/**
	 * Controls the current motion of the activity marker.
	 */
	public void spin() 
	{
		if (my_current_rotation == 0)
		{
			my_current_rotation = 1;
			my_key_spiner = my_spiner1;
		}
		else if (my_current_rotation == 1)
		{
			my_current_rotation = 2;
			my_key_spiner = my_spiner2;
		}
		else if (my_current_rotation == 2)
		{
			my_current_rotation = 3;
			my_key_spiner = my_spiner3;
		}
		else if (my_current_rotation == 3)
		{
			my_current_rotation = 0;
			my_key_spiner = my_spiner;
		}
	}

}
